<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理</title>
    <script src="lib/vue.js"></script>
    <style>
        input:disabled {
            background: peachpuff;
        }

        .errorInfo {
            color: red;
            font-size: 12px;
        }

        .grid {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }

        h1 {
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="grid">
            <div>
                <h1>图书管理</h1>
                <div class="book">
                    <div>
                        <label for='id'>编号:</label>
                        <input type="text" v-model="id" :disabled='flag'>
                        <label for="name">名称:</label>
                        <input type="text" v-model="name">
                        <button @click='handleSubmit' disabled='submitFlag'>提交</button>
                        <span class="errorInfo" ref='errorInfo'></span>
                    </div>
                </div>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in books" :key="item.id">
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.date}}</td>
                        <td>
                            <a href="#" @click.prevent="edit(item.id)">修改</a>
                            <span>|</span>
                            <a href="#" @click.prevent='del(item.id)'>删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <script>
        new Vue({
            el: "#app",
            data: {
                id: '',
                name: '',
                flag: false,
                submitFlag: false,
                books: [{
                        id: 1,
                        name: '三国演义',
                        date: ''
                    },
                    {
                        id: 2,
                        name: '水浒传',
                        date: ''
                    },
                    {
                        id: 3,
                        name: '红楼梦',
                        date: ''
                    },
                    {
                        id: 4,
                        name: '西游记',
                        date: ''
                    }
                ]
            },
            methods: {
                edit(id) {
                    //禁止修改id
                    this.flag = true;
                    console.log('edit');
                    console.log(id);
                    //根据id查询出要编辑的数据
                    var book = this.books.filter(function (item) {
                        return item.id == id;
                    })
                    console.log(book);
                    //把获取的信息填充到表单
                    this.id = book[0].id;
                    this.name = book[0].name;
                },
                del(id) {
                    console.log("delete");
                    //根据id查找数组中的的元素索引
                    var index = this.books.findIndex(function (item) {
                        return item.id == id
                    })
                    console.log(index);
                    //根据索引删除数组元素
                    //方法一
                    // this.books.splice(index, 1)
                    //方法二
                    this.books = this.books.filter(function (item) {
                        return item.id != id
                    })
                },
                handleSubmit() {
                    if (this.flag) {
                        //编辑操作
                        //根据当前的id去更新数组中的数据
                        this.books.some((item) => {
                            if (item.id == this.id) {
                                item.name = this.name
                                return true;
                            }
                        })
                        this.flag = false
                        this.id = ''
                        this.name = ''
                    } else {
                        //添加操作
                        var book = {};
                        book.id = this.id;
                        book.name = this.name;
                        book.date = '';
                        this.books.push(book);
                        //清空表单
                        this.id = '';
                        this.name = '';
                    }
                }
            },
            watch: {
                name: function (val) {
                    var flag = this.books.some(function (item) {
                        return item.name == val;
                    });
                    console.log(this.$refs.errorInfo);
                    console.log(flag);
                    if (flag) {
                        this.submitFlag = true;
                        this.$refs.errorInfo.innerHTML = '图书名称已存在！'
                    } else {
                        this.submitFlag = false;
                        this.$refs.errorInfo.innerHTML = ''
                    }
                },
                id: function (val) {
                    var idFlag = this.books.some(function (item) {
                        console.log(item);
                        return item.id == val;
                    })
                    if (idFlag) {
                        this.submitFlag = true;
                        this.$refs.errorInfo.innerHTML = '图书id已存在！'
                    } else {
                        this.submitFlag = false;
                        this.$refs.errorInfo.innerHTML = ''
                    }
                }
            }
        })
    </script>

</body>

</html>